package eerbaar.v2.app.view2;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

import eerbaar.v2.app.core.ISprite;
import eerbaar.v2.graphics.rect.RectCenter;

public class SlideImage {

    private int slide = 1;
    private int count;
    private final ISprite image;
    private final int oHeight;
    private final int oWidth;
    private final int slides;

    private final int slideWidth;

    public SlideImage(ISprite image) {
        this.image = image;

        this.oHeight = this.image.getHeight();
        if (image.getWidth() % image.getWidth() == 0) {
            this.oWidth = this.image.getWidth();
        } else {
            this.oWidth = this.oHeight;
        }

        this.slides = (this.oWidth / this.oHeight);
        this.slideWidth = this.oWidth / this.slides;
    }

    public void drawImage(Rectangle current, Graphics2D g) {

        if (this.slide >= this.slides || this.slide <= 0) {
            this.slide = 1;
        }
        int slideOffset = (this.slide - 1) * this.slideWidth;
        this.count++;
        if (this.count > 7) {
            this.slide++;
            this.count = 0;
        }

        Point p = RectCenter.calcTopLeft(current, this.oHeight, this.oHeight);

        int sx = slideOffset;
        int sy = 0;
        int sWidth = this.image.getHeight();
        int sHeight = this.image.getHeight();

        int dx = p.x;
        int dy = p.y;
        int dWidth = this.image.getHeight();
        int dHeight = this.image.getHeight();

        // Graphics2D graphics = (Graphics2D) canvas.getBufferStrategy()
        // .getDrawGraphics();

        g.drawImage(this.image.getImage(), dx, dy, dx + sWidth, dy + sHeight,
                sx, sy, sx + dWidth, sy + dHeight, null);
    }

}
